/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package converters;

import entities.Country;
import facades.CountryFacade;
import javax.faces.bean.ApplicationScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author damfercer
 */

@Named
@ApplicationScoped
public class CountryConverter implements Converter{
    /*@Inject
    @ResourceList
    Map<Integer,Country> countriesMap;*/
    @Inject
    private CountryFacade countryFacade;
    
    @Override
    public Country getAsObject(FacesContext context, UIComponent component, String value) {
        Country country=null;
        try {
            Integer i=new Integer(value);
            //country=countriesMap.get(i);
            country=countryFacade.find(i);
        } catch (NumberFormatException e) {
        }
        return country;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String res=null;
        Country country=null;
        if(value.getClass().equals(Country.class)){
            country=(Country)value;
        }
        if(country!=null){
            res=country.getIdCountry().toString();
        }
        return res;
    }
    
}
